﻿
using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model.ENUM;
using Gift.Model;
using Gift.DAL;

namespace WebSite.admin.system
{
    public partial class navigatorEdit01 : BaseAdmin
    {
        readonly DALNavigator theNavigator = new DALNavigator();
        tbNavigator oNavigator;
        // Fields

        private void BindParentNavigatorList()
        {
            DataTable table = theNavigator.GetList("parentId=0").Tables[0];
            this.ddlNavigaterParent.DataTextField = "navigatorName";
            this.ddlNavigaterParent.DataValueField = "navigatorId";
            this.ddlNavigaterParent.DataSource = table;
            this.ddlNavigaterParent.DataBind();
            this.ddlNavigaterParent.Items.Add(new ExtAspNet.ListItem("项级栏目", "0"));
            this.ddlNavigaterParent.Items.Add(new ExtAspNet.ListItem("请选择上级栏目...", "-1"));
            this.ddlNavigaterParent.SelectedValue = "-1";
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.AddArticle);
                this.BindParentNavigatorList();

                this.BuildForm();
             
            }
        }


        void BuildForm()
        {
            if (Request.QueryString["nid"] != null)
            {
                oNavigator = theNavigator.GetModel(int.Parse(Request.QueryString["nid"]));
                this.txtTitle.Text = oNavigator.navigatorName;
                this.txtOrder.Text = oNavigator.navigatorOrder.ToString();
                this.txtNavigatorLink.Text = oNavigator.navigatorLink;
                int iVisible = oNavigator.visible ? 1 :0;
                this.ddlNavigaterParent.SelectedValue = this.ddlNavigaterParent.Items.FindByValue(oNavigator.parentId.ToString()).Value;
                this.rblIsVisible.SelectedValue = this.rblIsVisible.Items.FindByValue(iVisible.ToString()).Value;
            
                int iAllowDeleted = oNavigator.AllowDeleted ? 1 : 0;
                this.rblAllowDeleted.SelectedValue = this.rblAllowDeleted.Items.FindByValue(iAllowDeleted.ToString()).Value;
            }
        }

        protected void btnSubmitForm1_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["nid"] != null)
            {
                oNavigator = this.theNavigator.GetModel(int.Parse(Request.QueryString["nid"]));
            }
            else
            {
                oNavigator = new tbNavigator();
            }
            oNavigator.navigatorName = this.txtTitle.Text.Trim();
            oNavigator.navigatorOrder = int.Parse(this.txtOrder.Text);
            oNavigator.visible = this.rblIsVisible.SelectedItem.Value == "1" ? true : false;
            oNavigator.parentId = int.Parse(this.ddlNavigaterParent.SelectedItem.Value);
            oNavigator.navigatorLink = this.txtNavigatorLink.Text.Trim();
            oNavigator.AllowDeleted = this.rblAllowDeleted.SelectedItem.Value == "1" ? true : false;
            if (Request.QueryString["nid"] != null)
            {
                theNavigator.Update(oNavigator);
            }
            else
            {
              oNavigator.navigatorId =   theNavigator.Add(oNavigator);
            }

            ExtAspNet.Alert.Show("保存成功！");

        }

    }
}